package com.svo.weitu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.svo.weitu.R;
import com.svo.weitu.activity.fragment.UserGuideFragment;

public class UserGuide extends FragmentActivity {
	private int max = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_guide);
		MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager());

		final ViewPager mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);
		
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
				Intent intent = new Intent();
		        if (arg0 == 0) { // 表示滑动已经停止
		            if (max == 0) {
		                if (mPager.getCurrentItem() == mPager.getChildCount()-1) {
		                    intent.setClass(UserGuide.this,MainActivity.class);
		                    startActivity(intent);
		                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		                    finish();
		                }
		                return;
		            }
		            max = 0;
		        }
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (max == 0) {
		            max = Math.max(max, arg2);
		        }
			}
		});
	}
	class MyAdapter extends FragmentPagerAdapter{

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			if (arg0 == 0) {
				return UserGuideFragment.newInstance(R.drawable.logo);
			}else {
				return UserGuideFragment.newInstance(R.drawable.logo);
			}
		}

		@Override
		public int getCount() {
			return 2;
		}
	}
}
